﻿#include "ObjectManager.h"

//ATC/Codec/DeviceRegisterCodec
#include "DeviceRegisterCodec.h"
//ATC/Codec/
#include "MCUCodec.h"
#include "TransmitCodec.h"
#include "MEMSDataCodec.h"
#include "ReadMEMSDataCodec_Server.h"
#include "TcpCodec.h"
#include "TurnTableCodec.h"
#include "UdpCodec.h"
#include "UserRegisterCodec.h"
#include "HeartBeatCodec.h"
//ACT/DataBase
#include "DataBaseConnector.h"
#include "Mapper_Device.h"
#include "Mapper_MEMSData.h"
#include "Mapper_Test_RTT.h"
#include "Mapper_testDataByTime.h"
#include "Mapper_User.h"
//ACT/Manager/RegisterCenter
#include "RegisterCenter_Device.h"
#include "RegisterCenter_User.h"
//ACT/Manager
//#include "MsgIdManager.h"
//ACT
#include "UdpController.h"
//IO/TcpListener
#include "NewTcpIOHandler.h"
#include "TcpListener.h"
//IO
#include "UdpIOer.h"
//end
//#include "DataHandler.h"

//config file
#include "MacroDefinition.h"

#include "qdebug.h"
#include "TurnTableCode_TurnTable_Connect.h"
ObjectManager* ObjectManager::objectManager=Q_NULLPTR;

ObjectManager::ObjectManager()
{
  objectManager=this;
  objectManager->setObjectName("objectManager");
  objectCreation();
  objectInjection();
  objectInitialization();
  logSaver->saveLog("ObjectManager created");
}

ObjectManager::~ObjectManager()
{
  for(QString objName : objMap.keys())
    {
      removeFromObjMap(objName);
    }
}

ObjectManager* ObjectManager::getObjectManager()
{
  return Q_NULLPTR==objectManager?objectManager=new ObjectManager():objectManager;
}

void ObjectManager::objectCreation()
{
  logSaver->saveLog("-------------------------objectCreation-------------------------");
  //ATC/Codec/DeviceRegisterCodec
  addToObjMap<DeviceRegisterCodec>();
  //ATC/Codec
  addToObjMap<MCUCodec>();
  addToObjMap<TransmitCodec>();
//  addToObjMap<MEMSDataCodec>();
//  addToObjMap<ReadMEMSDataCodec_Server>();
  addToObjMap<TcpCodec>();
  addToObjMap<TurnTableCodec>();
  addToObjMap<UdpCodec>();
  addToObjMap<UserRegisterCodec>();
  addToObjMap<HeartBeatCodec>();
  //ACT/DataBase
  addToObjMap<DataBaseConnector>();
  addToObjMap<Mapper_Device>();
  addToObjMap<Mapper_MEMSData>();
  addToObjMap<Mapper_Test_RTT>();
  addToObjMap<Mapper_testDataByTime>();
  addToObjMap<Mapper_User>();
  //ACT/Manager/RegisterCenter
  addToObjMap<RegisterCenter_Device>();
  addToObjMap<RegisterCenter_User>();
  //ACT/Manager
  //  addToObjMap<MsgIdManager>();
  //ACT
  addToObjMap<UdpController>();
  //IO/TcpListener
  addToObjMap<NewTcpIOHandler>();
  addToObjMap<TcpListener>();
  //IO
  addToObjMap<UdpIOer>();
  //  addToObjMap<DataHandler>();
  logSaver->saveLog("-------------------------objectCreation-------------------------");
}

#ifdef OBJECTINJECTION_ON
void ObjectManager::objectInjection()
{
  logSaver->saveLog("-------------------------objectInjection-------------------------");
  for(QObject* obj:objMap.values())
    {
      ObjAutoInject* temp=dynamic_cast<ObjAutoInject*>(obj);
      if(temp)
        {
          temp->objectAutoInjection(&objMap);
        }
    }
  logSaver->saveLog("-------------------------objectInjection-------------------------");
}
#else
void ObjectManager::objectInjection()
{}
#endif

void ObjectManager::objectInitialization()
{
  logSaver->saveLog("-------------------------objectInitialization-------------------------");
  getObjFromMap<TcpListener>()->listen(QHostAddress::Any, LISTENPORT);
  getObjFromMap<UdpIOer>()->bind(LISTENPORT);
  getObjFromMap<DataBaseConnector>()->connectToDataBase();
  logSaver->saveLog("-------------------------objectInitialization-------------------------");
}

void ObjectManager::removeFromObjMap(QObject* obj)
{
  if(Q_NULLPTR==obj)
    {
      return;
    }
  removeFromObjMap(obj->objectName());
}

void ObjectManager::removeFromObjMap(QString objName)
{
  QString name=objName;
  if(name.isEmpty())
    {
      logSaver->saveLog(QString("removeFromObjMap , obj name is empty"));
      return;
    }
  if(!objMap.contains(name))
    {
      logSaver->saveLog(QString("removeFromObjMap , obj %1 is not in objMap").arg(name));
      return;
    }
  objMap.take(name)->deleteLater();
  logSaver->saveLog(QString("removeFromObjMap , obj %1 is removed from objMap").arg(name));
}

void ObjectManager::addToObjMap(QObject* obj,QString objName)
{
  if(Q_NULLPTR==obj)
    {
      logSaver->saveLog("addToObjMap , obj is null");
      return;
    }
  obj->setObjectName(objName);
  QString name=obj->objectName();
  if(name.isEmpty())
    {
      logSaver->saveLog(QString("addToObjMap , obj name is empty"));
      obj->deleteLater();
      return;
    }
  if(objMap.contains(name))
    {
      logSaver->saveLog(QString("addToObjMap , obj %1 is already in objMap").arg(name));

      obj->deleteLater();
      return;
    }
  objMap.insert(name,obj);
  logSaver->saveLog(QString("addToObjMap , obj %1 is added to objMap").arg(name));
}







